import store from '../store'
const regEx = /\s+/g

export default {
  inserted (el, bindings, vnode) {
    const btnPermissionValue = bindings.value
    // const boolean = vnode.context.$store.state.buttonPermission[btnPermissionValue]
    const boolean = store.state.buttonPermission[btnPermissionValue]
    // !boolean && el.parentNode.removeChild(el)
    if (!boolean) {
      let className = el.getAttribute('class') || ''
      className = className.concat(' hidden')
      el.setAttribute('class', className)
    }
  },

  update: function (el, bindings, vnode) {
    const btnPermissionValue = bindings.value
    const boolean = store.state.buttonPermission[btnPermissionValue]
    // !boolean && el.parentNode.removeChild(el)
    let className = el.getAttribute('class') || ''
    if (!boolean) {
      className = className.concat(' hidden')
    } else {
      className = className.replace('hidden', '').replace(regEx, '')
    }
    el.setAttribute('class', className)
  }
}
